﻿using RpcHelper;
using RpcModel;
using SqlExecHelper;
using SqlExecHelper.SetColumn;
using Wedonek.RpcStore.Service.Helper;
using Wedonek.RpcStore.Service.Model;

namespace Wedonek.RpcStore.Service.DAL
{
        /// <summary>
        /// 节点版本管理
        /// </summary>
        internal class ServiceVerConfigDAL : SqlBasicClass
        {
                public ServiceVerConfigDAL () : base ("ServiceVerConfig")
                {

                }

                public long Add (VerConfigDatum add)
                {
                        base.Insert (add, out long id);
                        return id;
                }

                public bool CheckIsRepeat (long rpcMerId, long sysTypeId, long verNum)
                {
                        return base.CheckIsExists (new ISqlWhere[] {
                                new SqlWhere("RpcMerId", System.Data.SqlDbType.BigInt){Value=rpcMerId},
                                new SqlWhere("VerNum", System.Data.SqlDbType.BigInt){Value=verNum},
                                new SqlWhere("SystemTypeId", System.Data.SqlDbType.BigInt){Value=sysTypeId}
                       });
                }
                internal void SetIsEnable (long id, bool isEnable)
                {
                        if (!base.Update (new ISqlSetColumn[] {
                                new SqlSetColumn("IsEnable", System.Data.SqlDbType.Bit){ Value=isEnable}
                        }, "Id", id))
                        {
                                throw new ErrorException ("rpc.ver.config.set.fail");
                        }
                }
                public ServiceVerConfig Get (long id)
                {
                        return base.GetRow<long, ServiceVerConfig> ("id", id);
                }
                public BasicServiceVer[] Query (VerQueryParam query, IBasicPage paging, out long count)
                {
                        paging.InitOrderBy ("Id", true);
                        return base.Query<BasicServiceVer> (paging.OrderBy, paging.Index, paging.Size, out count, query.ToWhere ());
                }
                public void Delete (long id)
                {
                        if (!base.Drop ("id", id))
                        {
                                throw new ErrorException ("rpc.ver.config.drop.fail");
                        }
                }
                public void Set (long id, ServiceVerSet set)
                {
                        if (!base.Update (set, "id", id))
                        {
                                throw new ErrorException ("rpc.ver.config.set.fail");
                        }
                }
        }
}
